const Config = {
    "imgSizeParams":{
        "m_fixed":"?x-oss-process=image/resize,m_fixed,h_300,w_300",
        "m_fill": "?x-oss-process=image/quality,Q_80/interlace,1",
        "m_resize":"?x-oss-process=image/quality,Q_80/interlace,1/resize,w_",
        "m_mfit": "?x-oss-process=image/resize,m_mfit,h_300,w_300"
      },
    //   "imgBaseUrl":"https://uatcdn.sharegoodsmall.com/sharegoods/resource/xcx/",
      "imgBaseUrl":"",
}
Component({
    properties: {
        imgSrc:{
            type: String,
            value: '',
            observer: '_onRefresh',
        },
        mode:{
            type:String,
            value:'widthFix'
        }, // 图片
        imgStyle: String, // 图片的样式
        width: Number, // 根据宽度裁剪线上的图片
    },
    data: {
        imgUrl: {
            imgStyle: '',
            isHttpImg: false,
            baseImgUrl: ''
        }
    },
    methods: {
        _onRefresh(newVal, oldVal){
            if (newVal != oldVal) {
                this.getBaseUrl(newVal)
            }
        },
        getBaseUrl(newVal) {
            let systemInfo = wx.getSystemInfoSync()
            let rate = systemInfo.pixelRatio
            let that = this
            let width = systemInfo.windowWidth
            if (this.data.width){
                width = (this.data.width) * (systemInfo.windowWidth / 750)
            }
            // 如果监听到有新的值 那么就用新的地址
            let imgSrc = newVal || that.data.imgSrc
            this.setImgSrc(imgSrc, Config.imgSizeParams.m_resize + Math.ceil(width*rate))
        },
        setImgSrc(imgSrc, imgMode) {
            let isHttpImg = false
            let baseImgUrl = ''
            if (imgSrc.includes('http')) { // 后台图片
                if(imgSrc.indexOf('.gif')==-1){
                    baseImgUrl = imgSrc + imgMode
                } else {
                    baseImgUrl = imgSrc
                }
                isHttpImg = true
                this.data.imgStyle = this.data.imgStyle + ";background:  #efefef"
            } else if (imgSrc.includes('/img/')) { // 本地图片
                baseImgUrl = imgSrc
            } else { // 存在现实的UI图片 由于小程序内存的限制
                baseImgUrl = Config.imgBaseUrl + imgSrc
                if (baseImgUrl.includes('?')) {
                    baseImgUrl += '&'
                } else {
                    baseImgUrl += '?'
                }
                baseImgUrl += + 'time=' + new Date().getTime()
            }
            this.setData({
                imgUrl: {
                    url: baseImgUrl,// 防止缓存的存在
                    isHttpImg: isHttpImg,
                    imgStyle: this.data.imgStyle
                }
            })
        },
        bindload(e) {
            this.setData({
                ['imgUrl.imgStyle']: this.data.imgStyle + ";background: transparent"
            })
        },
    },
    ready: function () {

    }
})
